#nullable enable
using System.Collections.Generic;
using System.Linq;
using Godot;

namespace AndroidUnevenRoadGame.Game
{
    public static class RewardSystem
    {
        private const int BaseGoldReward = 25;
        private const int EliteGoldBonus = 25;
        private const int BossGoldBonus = 50;
        private const int PotionDropChancePercent = 50;
        private const int CardRewardCount = 3;

        public static void ShowRewards(GameRoot game, MapNodeType nodeType)
        {
            var rewards = GenerateRewards(game, nodeType);
            RewardStore.Current = rewards;
            game.ChangeState(GameRoot.GameState.Victory);
        }

        private static RewardBundle GenerateRewards(GameRoot game, MapNodeType nodeType)
        {
            var bundle = new RewardBundle();

            var gold = BaseGoldReward + game.CurrentFloor * 2;
            if (nodeType == MapNodeType.Elite)
            {
                gold += EliteGoldBonus;
            }
            else if (nodeType == MapNodeType.Boss)
            {
                gold += BossGoldBonus;
            }
            bundle.Gold = gold;

            bundle.Cards = CardDatabase.GetRandomCards(CardRewardCount, game.Random)
                .ToList();

            if (game.Random.RandiRange(1, 100) <= PotionDropChancePercent)
            {
                bundle.PotionId = PotionDatabase.GetRandomPotionId(game.Random);
            }

            if (nodeType == MapNodeType.Elite)
            {
                bundle.RelicId = RelicDatabase.GetRandomRelicId(game.Random);
            }

            return bundle;
        }

        public sealed class RewardBundle
        {
            public int Gold { get; set; }
            public List<CardDefinition> Cards { get; set; } = new();
            public string? PotionId { get; set; }
            public string? RelicId { get; set; }
        }

        public static class RewardStore
        {
            public static RewardBundle Current { get; set; } = new RewardBundle();

            public static void Clear()
            {
                Current = new RewardBundle();
            }
        }
    }
}

